package com.example.meitu.api.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.meitu.api.domain.*;
import com.example.meitu.api.dto.AccountPlatformDTO;
import com.example.meitu.api.dto.MediaAccountDTO;
import com.example.meitu.api.dto.MediaAccountPlatformDTO;
import com.example.meitu.api.service.*;
import com.example.meitu.api.dao.MediaAccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author 蔡超
* @description 针对表【mt_media_account(媒体账号表)】的数据库操作Service实现
* @createDate 2024-06-27 15:26:06
*/
@Service
public class MediaAccountServiceImpl extends ServiceImpl<MediaAccountMapper, MediaAccount>
    implements MediaAccountService{

    @Resource
    private DictService dictService;

    @Resource
    private DictItemService dictItemService;

    @Autowired
    @Lazy
    private MediaService mediaService;

    @Resource
    private MediaAccountMapper mediaAccountMapper;

    @Lazy
    @Resource
    private ArticleService articleService;

    /**
     * 查询整个媒体 在各个平台，有哪些账号
     * @param mediaId
     * @return
     */
    @Override
    public List<MediaAccountPlatformDTO> accountList(Integer mediaId) {
        //查询媒体的所有账号
        LambdaQueryWrapper<MediaAccount> accountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        accountLambdaQueryWrapper.eq(MediaAccount::getMediaId, mediaId);
        accountLambdaQueryWrapper.eq(MediaAccount::getDelFlag, '0');
        List<MediaAccount> mediaAccounts = list(accountLambdaQueryWrapper);
        Map<String,List<MediaAccount>> map = mediaAccounts.stream().collect(Collectors.groupingBy(MediaAccount::getPlatform));

        //7个媒体源下的媒体列表
        List<String> dictTypeList = new ArrayList<>();
        dictTypeList.add("zimeiti");
        dictTypeList.add("duanshipin");
        dictTypeList.add("shemei");
        dictTypeList.add("xiaofei");
        dictTypeList.add("meiti");
        dictTypeList.add("zhengquan");
        dictTypeList.add("boke");
        LambdaQueryWrapper<DictItem> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(DictItem::getDelFlag, '0');
        lambdaQueryWrapper.in(DictItem::getDictType,dictTypeList);
        List<DictItem> dictItems = dictItemService.list(lambdaQueryWrapper);
        Map<String,List<DictItem>> dictItemMap = dictItems.stream().collect(Collectors.groupingBy(DictItem::getDictType));

        //7个媒体源
        LambdaQueryWrapper<Dict> dictWrapper = new LambdaQueryWrapper();
        dictWrapper.in(Dict::getDictType, dictTypeList);
        dictWrapper.eq(Dict::getDelFlag, '0');
        List<Dict> dictList = dictService.list(dictWrapper);

        List<MediaAccountPlatformDTO> list = new ArrayList<>();
        for (Dict dict : dictList) {
            MediaAccountPlatformDTO mediaAccountPlatformDTO = new MediaAccountPlatformDTO();
            mediaAccountPlatformDTO.setName(dict.getDescription());
            List<DictItem> dictItemsByType = dictItemMap.get(dict.getDictType());
            List<MediaAccountDTO> accountDTOS = new ArrayList<>();
            for (DictItem item: dictItemsByType) {
                MediaAccountDTO mediaAccountDTO = new MediaAccountDTO();
                mediaAccountDTO.setName(item.getItemValue());  //媒体的平台
                mediaAccountDTO.setData(map.get(item.getItemValue()) == null ? new ArrayList<>() : map.get(item.getItemValue()));   //媒体的账号列表
                accountDTOS.add(mediaAccountDTO);
            }
            mediaAccountPlatformDTO.setData(accountDTOS);
            list.add(mediaAccountPlatformDTO);
        }
        return list;
    }

    @Override
    public List<MediaAccountPlatformDTO> accountList2(Integer subUnionId,Integer startTime,Integer endTime) {

        LambdaQueryWrapper<Media> mediaLambdaQueryWrapper = new LambdaQueryWrapper();
        mediaLambdaQueryWrapper.eq(Media::getSubUnionMediaId, subUnionId);
        mediaLambdaQueryWrapper.eq(Media::getDelFlag, '0');
        List<Integer> mediaIds = mediaService.list(mediaLambdaQueryWrapper).stream().map(Media::getId).collect(Collectors.toList());

        //查询媒体的所有账号
        LambdaQueryWrapper<MediaAccount> accountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        accountLambdaQueryWrapper.in(MediaAccount::getMediaId, mediaIds);
        accountLambdaQueryWrapper.eq(MediaAccount::getDelFlag, '0');
        List<MediaAccount> mediaAccounts = list(accountLambdaQueryWrapper);
        Map<String,List<MediaAccount>> map = mediaAccounts.stream().collect(Collectors.groupingBy(MediaAccount::getPlatform));

        //7个媒体源下的媒体列表
        List<String> dictTypeList = new ArrayList<>();
        dictTypeList.add("zimeiti");
        dictTypeList.add("duanshipin");
        dictTypeList.add("shemei");
        dictTypeList.add("xiaofei");
        dictTypeList.add("meiti");
        dictTypeList.add("zhengquan");
        dictTypeList.add("boke");
        dictTypeList.add("qitapingtai");
        dictTypeList.add("bufenpingtai");
        LambdaQueryWrapper<DictItem> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.eq(DictItem::getDelFlag, '0');
        lambdaQueryWrapper.in(DictItem::getDictType,dictTypeList);
        lambdaQueryWrapper.orderByAsc(DictItem::getSortOrder);
        List<DictItem> dictItems = dictItemService.list(lambdaQueryWrapper);
        Map<String,List<DictItem>> dictItemMap = dictItems.stream().collect(Collectors.groupingBy(DictItem::getDictType));

        List<DictItem> bufenpingtai = dictItemMap.get("qitapingtai");
        List<String> bufenpingtaiList = bufenpingtai.stream().map(DictItem::getItemValue).collect(Collectors.toList());

        //查询文章表，资讯网站、其它平台号、其他客户端都有多少个
        LambdaQueryWrapper<Article> articleWrapper = new LambdaQueryWrapper();
        articleWrapper.eq(Article::getSubUnionId, subUnionId);
        articleWrapper.in(Article::getPlatform, bufenpingtaiList);
        articleWrapper.between(Article::getPublishTime, startTime, endTime);
        articleWrapper.eq(Article::getDelFlag, '0');
        List<Article> articleList = articleService.list(articleWrapper);
        // 将articleList按照publishPlatform进行分组，返回map
        Map<String, List<Article>> articleMap = articleList.stream().collect(Collectors.groupingBy(Article::getPlatform));

        //7个媒体源
        LambdaQueryWrapper<Dict> dictWrapper = new LambdaQueryWrapper();
        dictWrapper.in(Dict::getDictType, dictTypeList);
        dictWrapper.eq(Dict::getDelFlag, '0');
        dictWrapper.orderByAsc(Dict::getCreateTime);
        List<Dict> dictList = dictService.list(dictWrapper);

        List<MediaAccountPlatformDTO> list = new ArrayList<>();
        for (Dict dict : dictList) {
            MediaAccountPlatformDTO mediaAccountPlatformDTO = new MediaAccountPlatformDTO();
            mediaAccountPlatformDTO.setName(dict.getDescription());
            List<DictItem> dictItemsByType = dictItemMap.get(dict.getDictType());
            List<MediaAccountDTO> accountDTOS = new ArrayList<>();
            for (DictItem item: dictItemsByType) {
                MediaAccountDTO mediaAccountDTO = new MediaAccountDTO();
                mediaAccountDTO.setName(item.getItemValue());  //媒体的平台
                mediaAccountDTO.setData(map.get(item.getItemValue()) == null ? new ArrayList<>() : map.get(item.getItemValue()));   //媒体的账号列表

                //返回是否高亮,普通的根据data下是否有账号判断
                mediaAccountDTO.setExist(mediaAccountDTO.getData().size()>0 ? 1:0);
                // [其它平台] 根据是否有文章，有文章就高亮
                //资讯网站
                //其它平台号
                //其他客户端
                if (CollectionUtil.isNotEmpty(articleMap.get(item.getItemValue()))){
                    mediaAccountDTO.setExist(1);
                }
                // 不分平台 默认高亮
                if (item.getItemValue().equals("不分平台")){
                    mediaAccountDTO.setExist(1);
                }

                accountDTOS.add(mediaAccountDTO);
            }
            mediaAccountPlatformDTO.setData(accountDTOS);
            list.add(mediaAccountPlatformDTO);
        }
        return list;
    }

    @Override
    public List<AccountPlatformDTO> getAllAccountPlatforms() {
        return mediaAccountMapper.getAllAccountPlatforms();
    }

}




